/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package icexam.dao;

import icexam.user.UserBean;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ethan
 */
public class UserDao {

    public boolean haveThisUser(UserBean user) {
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("select * from iuser where compname like ? and ceoname like ? and idcard like ?");
            statement.setString(1, user.getCompName());
            statement.setString(2, user.getCeoName());
            statement.setString(3, user.getIdcard());
            ResultSet rs = statement.executeQuery();
            if(rs.next()){
                user.setUid(rs.getInt(1));
                user.setSpentTime(rs.getInt(8));
                user.setBestScore(rs.getInt(9));
                return true;
            }
            else
                return false;
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
        
    }

    public UserBean createUser(UserBean user) {

        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("insert into iuser(compname,ceoname,idcard,admin) values(?,?,?,0)");
            statement.setString(1, user.getCompName());
            statement.setString(2, user.getCeoName());
            statement.setString(3, user.getIdcard());
            statement.execute();
            statement = conn.prepareStatement("select uid from iuser where compname like ?");
            statement.setString(1, user.getCompName());
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                user.setUid(rs.getInt(1));
                return user;
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        return null;
    }
    public static void main(String[] args){
//        try {
//            Connection conn = DBConnect.getConnection();
//            PreparedStatement statement = null;
//                statement = conn.prepareStatement("select now()");
//            ResultSet rs = statement.executeQuery();
//            while(rs.next())
//            System.out.println(rs.getString(1));
//        } catch (SQLException ex) {
//            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
//        }
        System.out.println(Math.round(Math.random()*9));

    }
    public void updateScore(UserBean user) {
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("update iuser set bestscore = ? where uid = ?");
            statement.setInt(1, user.getBestScore());
            statement.setInt(2, user.getUid());
//            statement.setString(3, user.getIdcard());
            statement.execute();

        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean companyErrorBoss(UserBean user) {
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("select * from iuser where compname like ? and (ceoname not like ? or idcard not like ?)");
            statement.setString(1, user.getCompName());
            statement.setString(2, user.getCeoName());
            statement.setString(3, user.getIdcard());
            ResultSet rs = statement.executeQuery();
            if(rs.next()){
                
                return true;
            }
            else
                return false;
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public void updateTime(UserBean user) {
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("update iuser set studytime = ? where uid = ?");
            statement.setInt(1, user.getSpentTime());
            statement.setInt(2, user.getUid());
            statement.execute();
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<UserBean> searchUser(UserBean user) {
        ArrayList<UserBean> list = new ArrayList<UserBean>();
        UserBean bean = null;
        try {
            Connection conn = DBConnect.getConnection();
            String sql = "select * from iuser where 1=1 ";
            if(user.getCompName()!=null&&!user.getCompName().equals("")){
                sql = sql+"and compname like '"+user.getCompName()+"'";
            }
            if(user.getCeoName()!=null&&!user.getCeoName().equals("")){
                sql = sql+" and ceoname like  '"+user.getCeoName()+"'";
            }
            if(user.getIdcard()!=null&&!user.getIdcard().equals("")){
                sql = sql+ " and idcard like '"+user.getIdcard()+"'";
            }
            System.out.println(sql);
            PreparedStatement statement = conn.prepareStatement(sql);
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                bean = new UserBean();
                bean.setUid(rs.getInt(1));
                bean.setCompName(rs.getString(2));
                bean.setCeoName(rs.getString(3));
                bean.setIdcard(rs.getString(4));
                bean.setSpentTime(rs.getInt(8));
                bean.setBestScore(rs.getInt(9));
                list.add(bean);
//                bean.set
            }
            return list;
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

}
